import '../style/index.scss'
import { rejects } from 'assert';

let inputs = document.querySelectorAll('input');
let btn = document.querySelector('.btn');
btn.onclick = (e) => {
    let user = inputs[0].value;
    let pwd = inputs[1].value;
    let ajax = () => {
        return new Promise((resolve, reject) => {
            let data = {
                    "user": user,
                    'pwd': pwd
                }
                // console.log(JSON.stringify(data))
            let xhr = new XMLHttpRequest();
            xhr.open('post', '/api/user', true);
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
            xhr.onreadystatechange = () => {
                if (xhr.readyState != 4) return
                if (xhr.status == 200) {
                    // console.log(JSON.parse(xhr.responseText))
                    let data = JSON.parse(xhr.responseText)
                    resolve(data.data)
                }
            }
            xhr.send(`user=${data.user}&pwd=${data.pwd}`)
        })
    }



    ajax().then((res) => {
        alert(res)
    }).catch((error) => {
        alert(error)
    })
}